﻿using UnityEngine;
using System.Collections;

public class TouchCamera : MonoBehaviour
{


    public GameObject target;

    public float distance = 10.0f;



    public float xSpeed = 250.0f;

    public float ySpeed = 120.0f;

    Vector3 tmp;

    public float yMinLimit = -20;

    public float yMaxLimit = 80;



    public float x = 0.0f;

    public float y = 0.0f;



    private Vector2 oldPosition1;

    private Vector2 oldPosition2;
    public float maxDistance = 600f;

    void Start()
    {

        Vector2 angles = transform.eulerAngles;

        x = angles.y;

        y = angles.x;


        // Make the rigid body not change rotation
        //if (rigidbody)
        //  rigidbody.freezeRotation = true;

    }

    void Update()
    {

        if (Input.touchCount == 1)

        {

            if (Input.GetTouch(0).phase == TouchPhase.Moved)

            {
                x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;

                y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;

                y = ClampAngle(y, yMinLimit, yMaxLimit);
                
            }

        }else if (Input.touchCount > 1)
        {
            if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)

            {
                Vector3 tempPosition1 = Input.GetTouch(0).position;

                Vector3 tempPosition2 = Input.GetTouch(1).position;

                if (isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2))

                {

                    if (distance > 3)

                    {

                        distance -= 0.5f;

                    }   //Unity3D教程手册：www.manew.com

                }
                else

                {

                    if (distance < maxDistance)

                    {

                        distance += 0.5f;

                    }

                }

                oldPosition1 = tempPosition1;

                oldPosition2 = tempPosition2;

            }

        }

    }



    bool isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)

    {

        float leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));

        float leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));

        if (leng1 < leng2)

        {

            return true;

        }
        else

        {

            return false;

        }

    }  //Unity3D教程手册：www.manew.com

    public void LateUpdate()
    {

        if (target && Input.touchCount>0)
        {
            ClampAngle(y, yMinLimit, yMaxLimit);

            Quaternion rotation = Quaternion.Euler(y, x, 0);




            tmp.Set(0.0f, 0.0f, (-1) * distance);

            Vector3 position = rotation * tmp + target.transform.position;




            transform.rotation = rotation;

            transform.position = position;

        }

    }

    static float ClampAngle(float angle, float min, float max)
    {

        
        if (angle < -360)
        
            angle += 360;

        if (angle > 360)

            angle -= 360;
        
        return Mathf.Clamp(angle, min, max);

    }



}
 
